package com.richpay.basejava.base10;

/**
 * Java的clone
 */
public class CloneDemo {
    public static void main(String[] args) throws CloneNotSupportedException {

        /**
         * 浅克隆
         */
        Dog d1 = new Dog("小黄");
        Object obj = d1.clone();//将d1对象在内存中完整的复制了一份新的对象。
        if (obj instanceof Dog) {
            Dog d2 = (Dog) obj;
            d2.setName("小花");
            System.out.println("原始对象:" + d1);
            System.out.println("克隆之后的对象:" + d2);
        }

        System.out.println("================================");

        /**
         * 深度克隆
         */
        Employee employee1 = new Employee("张三", 20);
        Company company1 = new Company("丰付", 21, employee1);

        Company company2 = (Company) company1.clone();
        company2.setName("千峰教育");
        company2.setAge(40);
        company2.getEmployee().setName("李四");
        company2.getEmployee().setAge(25);

        System.out.println(company1);
        System.out.println(company2);


    }
}
